<?php 
    class School_learn_card_model extends CI_Model{

        function __construct(){
            parent::__construct();
        }

        function get_learn_card($school_id,$limit = 0,$offset = 10,$status = -1 ){
            $where = "card.school_id = '{$school_id}' ";
            if($status>-1) $where .= " AND card.is_activation = $status ";

            $this->db->trans_start();
            $sql = "SELECT SQL_CALC_FOUND_ROWS card.*,u.nick FROM {$this->db->dbprefix}o2o_school_learn_card AS card ";
            $sql .= "LEFT JOIN {$this->db->dbprefix}user AS u ON(u.id = card.user_id) ";
            $sql .= "WHERE $where order by id desc LIMIT $limit, $offset";

            $result = $this->db->query($sql)->result_array();
            $row_num =  $this->db->query('SELECT FOUND_ROWS() as row_num;')->row_array();

            $this->db->trans_complete();

            return array("total_count" => $row_num['row_num'], "result" => $result);
        }




        function generate_learn_card($data,$school_id){

            $course = empty($data['course'])? array():$data['course'];
            $tiku = empty($data['tiku'])? array():$data['tiku'];
            $shixun = empty($data['shixun'])? array():$data['shixun'];

            $this->db->trans_start();
            
            for ($i=0; $i < $data['num']; $i++) { 

                //生成卡号
                $yCode = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J');
                $code = $yCode[rand(0, 9)] . strtoupper(dechex(date('m'))) . date('d') . substr(time(), -5) . substr(microtime(), 2, 5) . sprintf('%02d', rand(0, 99));

                //插入学习卡
                $card = array(
                    'title' => $data['title'], 
                    'school_id' => $school_id,
                    'card_num' => $code, 
                    'card_pwd' => getRandChar(6), 
                    'expire_day' => $data['expire_day'],
                    'user_id' => 0,
                    'price' => $data['price'],
                    'activation_day' => 0,
                    'activation_time' => 0,
                    'is_activation' => 0,
                    'add_time' => time(),
                    );
                $this->db->insert('o2o_school_learn_card',$card);
                $card_id = $this->db->insert_id();
                //插入学习卡明细
                if(!empty($course)){
                    foreach ($course as $cour_id) {
                        $card_item = array(
                            'card_id' => $card_id, 
                            'content_type' => 1, 
                            'content_id' => $cour_id, 
                            'add_time' => time(), 
                            );
                        $this->db->insert('o2o_school_learn_card_item',$card_item);
                    }
                }
                if(!empty($tiku)){
                    foreach ($tiku as $cour_id) {
                        $card_item = array(
                            'card_id' => $card_id, 
                            'content_type' => 4, 
                            'content_id' => $cour_id, 
                            'add_time' => time(), 
                            );
                        $this->db->insert('o2o_school_learn_card_item',$card_item);
                    }
                }
                if(!empty($shixun)){
                    foreach ($shixun as $cour_id) {
                        $card_item = array(
                            'card_id' => $card_id, 
                            'content_type' => 5, 
                            'content_id' => $cour_id, 
                            'add_time' => time(), 
                            );
                        $this->db->insert('o2o_school_learn_card_item',$card_item);
                    }
                }
            }
           
            $this->db->trans_complete();

          
            return 1;
        }
    }